package com.zshield.entry;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import com.zshield.util.Etime;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class FileSystemInOutEntry implements EntryInterface<FileSystemInOutEntry>{
    @SerializedName("SENSOR_ID")
    private String sensorId;
    @SerializedName("EXE_NAME")
    private String exeName;
    @SerializedName("ACCESS_FORMAT")
    private String accessFormat;
    @SerializedName("ACCESS_TIMES")
    private int accessTimes;
    @SerializedName("ACCESS_BYTES")
    private long accessBytes;
    @SerializedName("REMOTE_IP")
    private Set<String> remoteIp;
    @SerializedName("TIME")
    private String time;
    @SerializedName("DAY_TIME")
    private String dayTime;
    @SerializedName("@timestamp")
    private String timeStamp;
    @SerializedName("TIME_hms")
    private String timeHms;
    @SerializedName("PTYPE")
    private String ptype;

    public FileSystemInOutEntry (JsonObject obj,String fsAction,String accFormat,boolean isUpgrade) {
        sensorId = obj.get("SENSOR_ID").getAsString();
        exeName = obj.get("EXE_NAME").getAsString();
        String logTime = obj.get("TIME").getAsString();
        Etime etime = new Etime();
        time = etime.zoneParse(logTime);
        LocalDateTime ldt = LocalDateTime.parse(time, Etime.DATETIME_FORMATTER);
        dayTime = ldt.format(Etime.DATE_FORMATTER);
        timeStamp = LocalDateTime.now().format(Etime.DATETIME_FORMATTER);
        timeHms = ldt.format(Etime.TIME_FORMATTER);
        ptype = "FileInOut";
        accessTimes = 1;
        accessBytes = obj.get("FILE_SIZE").getAsLong();
        remoteIp = new HashSet<>();
        Iterator<JsonElement> it = obj.getAsJsonArray("REMOTE_IP").iterator();
        while (it.hasNext()) {
            String rip = it.next().getAsString();
            remoteIp.add(rip);
        }
        accessFormat = accFormat;
    }

    public FileSystemInOutEntry (JsonObject obj,String accFormat) {
        sensorId = obj.get("SENSOR_ID").getAsString();
        exeName = obj.get("EXE_NAME").getAsString();
        String logTime = obj.get("TIME").getAsString();
        Etime etime = new Etime();
        time = etime.zoneParse(logTime);
        LocalDateTime ldt = LocalDateTime.parse(time, Etime.DATETIME_FORMATTER);
        dayTime = ldt.format(Etime.DATE_FORMATTER);
        timeStamp = LocalDateTime.now().format(Etime.DATETIME_FORMATTER);
        timeHms = ldt.format(Etime.TIME_FORMATTER);
        ptype = "FileInOut";
        accessTimes = 1;
        accessBytes = obj.get("FILE_SIZE").getAsLong();
        accessFormat = accFormat;
        remoteIp = new HashSet<>();
        Iterator<JsonElement> it = obj.getAsJsonArray("REMOTE_IP").iterator();
        while (it.hasNext()) {
            String rip = it.next().getAsString();
            remoteIp.add(rip);
        }
    }

    public FileSystemInOutEntry (JsonObject obj) {
        sensorId = obj.get("SENSOR_ID").getAsString();
        exeName = obj.get("EXE_NAME").getAsString();
        String logTime = obj.get("TIME").getAsString();
        Etime etime = new Etime();
        time = etime.zoneParse(logTime);
        LocalDateTime ldt = LocalDateTime.parse(time, Etime.DATETIME_FORMATTER);
        dayTime = ldt.format(Etime.DATE_FORMATTER);
        timeStamp = LocalDateTime.now().format(Etime.DATETIME_FORMATTER);
        timeHms = ldt.format(Etime.TIME_FORMATTER);
        ptype = "FileInOut";
        accessTimes = obj.get("PRINT_COUNT").getAsInt();
        accessBytes = 0;
        accessFormat = "PRINT";
        remoteIp = new HashSet<>();
        remoteIp.add("");
    }

    public void update(FileSystemInOutEntry that){
        this.accessTimes = this.accessTimes + that.accessTimes;
        this.accessBytes = this.accessBytes + that.accessBytes;
        this.time = that.time;
    }

    @Override
    public String getDayTime() {
        return this.dayTime;
    }

    public String getDocId () {
        return "FileInOut~" + sensorId + "~" + accessFormat + "~" + remoteIp + "~" + exeName + "~" + dayTime;
    }

}
